#include "tolua_framework.h"

#include <stdlib.h>
#include <sstream>

TOLUA_API void toluafix_push_longlong(lua_State* L, void* ptr, const char* type)
{
	tolua_pushnumber(L, *((LUA_NUMBER*)ptr));
}

TOLUA_API int toluafix_is_longlong(lua_State* L, int lo, const char* type, int def, tolua_Error* err)
{
	if (def && lua_gettop(L)<abs(lo))
		return 1;
	if (lua_isnumber(L,lo))
		return 1;

	err->index = lo;
	err->array = 0;
	err->type = "long long";
	return 0;
}

TOLUA_API long long toluafix_to_longlong(lua_State* L, int narg, int def)
{	
	if (lua_gettop(L)<abs(narg))
		return def;

	lua_Number t = lua_tonumber(L, narg);
	return *((long long*)&t);
}

TOLUA_API double longlong2num(double num)
{
	long long *pll = (long long*)&num;
	return double(*pll);
}

TOLUA_API double num2longlong(double num)
{
	long long ll = (long long)num;

	return *((double*)&ll);
}

std::string longlong2str( double num )
{
	long long* pll = (long long*)&num;
	std::stringstream stream;
	stream << *pll;
	return stream.str();
}

double str2longlong( std::string str )
{
	std::istringstream stream(str);
	long long result = 0;
	stream >> result;
	return *((double*)&result);
}
